<?php
namespace app\common\controller;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

class Extend extends Backend
{
    //获取excel的数据
    public function getdata($filename){
        $filelink = request()->file($filename);
        $info = $filelink->move(ROOT_PATH.'/public/'.DS.'uploads/fujian');
        //获取文件名
        $file = ROOT_PATH.'public'.DS.'uploads/fujian'.DS.$info->getsaveName();
        /* 转码 */
        $file = iconv("utf-8", "gb2312", $file);

        if (empty($file) OR !file_exists($file)) {
            throw new \Exception('文件不存在!');
        }

        /** @var Xlsx $objRead */
        $objRead = IOFactory::createReader('Xlsx');

        if (!$objRead->canRead($file)) {
            /** @var Xls $objRead */
            $objRead = IOFactory::createReader('Xls');

            if (!$objRead->canRead($file)) {
                throw new \Exception('只支持导入Excel文件！');
            }
        }

        /* 如果不需要获取特殊操作，则只读内容，可以大幅度提升读取Excel效率 */
        empty($options) && $objRead->setReadDataOnly(true);
        /* 建立excel对象 */
        $obj = $objRead->load($file);
        /* 获取指定的sheet表 */
        $currSheet = $obj->getSheet(0)->toArray();
//        unset($currSheet[0]);
        return $currSheet;
    }
}